package com.cuit.jifver.weibo.db;


import com.cuit.jifver.weibo.WeiboApp;
import com.cuit.jifver.weibo.data.Weibo;
import com.cuit.jifver.weibo.utils.ConfigUtil;

import android.content.Context;
import android.content.SharedPreferences;

public class AppRecordData {
	//sharedperfrences name
	private final static String WEIBO_APP_PERFRENCE = "setting";
	
	//Visitor Id & Session Id
	private final static String VISITOR_ID_PREFER = "visitorId";
	private final static String SESSION_ID_PREFER = "sessionId";
	
	//Login record
	private final static String FIRST_LOGIN_PREFER = "loginRecord";
	//user Sign record, older or new
	private final static String SIGN_PREFER = "signRecord";
	
	
	//is first entry this application
	public static void setIsFirstLogin(Context context, boolean isfirst)
	{
		SharedPreferences setting = context.getSharedPreferences(WEIBO_APP_PERFRENCE, 0);
		SharedPreferences.Editor edit = setting.edit();
		edit.putBoolean(FIRST_LOGIN_PREFER, isfirst);
		edit.commit();
	}
	
	public static boolean IsFirstLogin(Context context)
	{
		SharedPreferences setting = context.getSharedPreferences(WEIBO_APP_PERFRENCE, 0);
		return setting.getBoolean(FIRST_LOGIN_PREFER, true);
	}
	
	//set visitor is older user
	public static void setOlderSuccess(Context context, boolean isOlder)
	{
		SharedPreferences setting = context.getSharedPreferences(WEIBO_APP_PERFRENCE, 0);
		SharedPreferences.Editor edit = setting.edit();
		edit.putBoolean(SIGN_PREFER, isOlder);
		edit.commit();
	}
	//get user is older or new user
	public static boolean IsOlder(Context context)
	{
		SharedPreferences setting = context.getSharedPreferences(WEIBO_APP_PERFRENCE, 0);
		return setting.getBoolean(SIGN_PREFER, false);
	}

	//create visitor ID
	public static void setVisitorId(String id)
	{
		SharedPreferences setting = WeiboApp.getInstance().getSharedPreferences(WEIBO_APP_PERFRENCE, 0);
		SharedPreferences.Editor edit = setting.edit();
		edit.putString(VISITOR_ID_PREFER,id);
		edit.commit();
	}
	
	public static String getVisitorId()
	{
		SharedPreferences setting = WeiboApp.getInstance().getSharedPreferences(WEIBO_APP_PERFRENCE, 0);
		return setting.getString(VISITOR_ID_PREFER, null);
	}
	
	//create session id
	public static void setSessionId(String id)
	{
		SharedPreferences setting = WeiboApp.getInstance().getSharedPreferences(WEIBO_APP_PERFRENCE, 0);
		SharedPreferences.Editor edit = setting.edit();
		edit.putString(SESSION_ID_PREFER,id);
		edit.commit();
	}
	
	public static String getSessionId()
	{
		SharedPreferences setting = WeiboApp.getInstance().getSharedPreferences(WEIBO_APP_PERFRENCE, 0);
		return setting.getString(SESSION_ID_PREFER, null);
	}
	
	
	/**
	 * save AccessToken and AccessTokenSecret
	 * @param token
	 * @param secret
	 */
	public static void setAccessToken(String token, String secret)
	{
		SharedPreferences setting = WeiboApp.getInstance().getSharedPreferences(WEIBO_APP_PERFRENCE, 0);
		SharedPreferences.Editor edit = setting.edit();
		edit.putString(ConfigUtil.access_token_name, token);
		edit.putString(ConfigUtil.access_token_secret_name, secret);
		edit.commit();
	}
	
	/**
	 * is user authorized
	 * @return
	 */
	public static boolean isUserAuthorized()
	{
		SharedPreferences setting = WeiboApp.getInstance().getSharedPreferences(WEIBO_APP_PERFRENCE, 0);
		String accessToken = setting.getString(ConfigUtil.access_token_name, null);
	    String accessTokenSecret = setting.getString(ConfigUtil.access_token_secret_name, null);
	    if (accessToken != null && accessTokenSecret != null)
		{
			
			Weibo weibo = Weibo.getInstance();
			weibo.setAccessToken(accessToken);
			weibo.setAccessSecret(accessTokenSecret);
			return true;
		}		
		return false;
	}
	
}
